package com.coffee.admin.controller.supplier;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.api.common.service.RedisService;
import com.coffee.admin.controller.BaseController;
import com.coffee.admin.excel.SkuOrderResultExcel;
import com.coffee.admin.utils.WXPayUtil;
import com.coffee.admin.utils.WeixinGzhAppletsUtil;
import com.coffee.core.anotation.OperLog;
import com.coffee.core.anotation.SupplierLoginCheck;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.RoleTypeEnum;
import com.coffee.core.enums.SkuOrderStatusEnum;
import com.coffee.core.enums.SkuTypeEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.result.Result;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.DateUtil;
import com.coffee.core.util.ExcelUtiles;
import com.coffee.order.api.inquire.result.WeiXinPushResult;
import com.coffee.order.api.sku.req.PlatformOrderListReq;
import com.coffee.order.api.sku.result.SkuApplyOrderResult;
import com.coffee.order.api.sku.result.SkuOrderDeliveryDemandResult;
import com.coffee.order.api.sku.result.SkuOrderResult;
import com.coffee.order.api.sku.result.SkuOrderResultExport;
import com.coffee.order.api.sku.service.SkuOrderService;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("/supplier/order")
public class SupplierOrderController extends BaseController {
    private final static Logger logger = LoggerFactory.getLogger(SupplierOrderController.class);

    @DubboReference(check = false)
    private SkuOrderService skuOrderService;
    @DubboReference(check = false)
    private RedisService redisService;

    @Value("${parameter.adminWebDomain}")
    private String webDomain;
    @Value("${parameter.webDomain}")
    private String web;

    private ExecutorService executorService = Executors.newFixedThreadPool(10);


    @OperLog(operModul = "平台-订单管理-订单管理", operDesc = "分页查询订单管理列表")
    @SupplierLoginCheck(alias = "SupplierOrderListAlias")
    @RequestMapping(value = "/findList", method = {RequestMethod.POST})
    public Result<Page<SkuOrderResult>> findList(HttpServletRequest request, @RequestBody PlatformOrderListReq req) {
        Result<Page<SkuOrderResult>> result = Result.success(null);
        PagerInfo pagerInfo = setDefaultPageInfo(req.getPageSize(), req.getPageIndex());
        req.setSupplierId(getSupplier().getSupplierId());
        req.setNoType(SkuTypeEnum.INQUIRE.getCode());
        try {
            result.setData(skuOrderService.getOrderListResultList(req, pagerInfo));
        } catch (BusinessException e) {
            e.printStackTrace();
        }
        return result;
    }

    @OperLog(operModul = "平台-订单管理-订单管理", operDesc = "分页查询订单管理列表")
    @SupplierLoginCheck(alias = "SupplierOrderListAlias")
    @RequestMapping(value = "/findList/export", method = {RequestMethod.POST})
    public void export(HttpServletRequest request, HttpServletResponse response, @RequestBody PlatformOrderListReq req) {
        Result<Page<SkuOrderResult>> result = Result.success(null);
        PagerInfo pagerInfo = setDefaultPageInfo(Integer.MAX_VALUE, 1);
        req.setSupplierId(getSupplier().getSupplierId());
        req.setNoType(SkuTypeEnum.INQUIRE.getCode());
        try {
            List<SkuOrderResultExport> orderListResultListExport = skuOrderService.getOrderListResultListExport(req, pagerInfo);
            // 导出Excel文件
            List<SkuOrderResultExcel> excelList = DTOUtils.convertList(orderListResultListExport, SkuOrderResultExcel.class);
            ExcelUtiles.exportExcel(excelList, "订单列表", "sheet1", SkuOrderResultExcel.class,
                    "order.xls", true, response);
        } catch (BusinessException e) {
            e.printStackTrace();
        }
    }


    @OperLog(operModul = "平台-订单管理-订单详情", operDesc = "订单详情")
    @SupplierLoginCheck(alias = "SupplierOrderListAlias")
    @RequestMapping(value = "/detail")
    public Result<SkuOrderResult> detail(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("skuOrderId");
        Result<SkuOrderResult> result = Result.success(null);
        SkuOrderResult detail = null;
        try {
            detail = skuOrderService.getSkuOrderResultDetailBySupplier(id, getSupplier().getSupplierId(), RoleTypeEnum.SUPPLIER.getCode());
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.failed(e.getCode(), e.getMessage());
        }
        result.setData(detail);
        return result;
    }

    @OperLog(operModul = "平台-订单管理-订单详情", operDesc = "先享后付批量完成")
    @SupplierLoginCheck(alias = "SupplierOrderOperationAlias")
    @RequestMapping(value = "/finishEnjoyOrder")
    public Result<String> finishEnjoyOrder(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        JSONArray skuIdJSONArray = jsonObject.getJSONArray("skuIds");
        List<Integer> skuIds = JSONObject.parseArray(skuIdJSONArray.toJSONString(), Integer.class);
        Result<String> result = Result.success(null);
        skuOrderService.finishEnjoyOrder(skuIds);
        result.setData("success");
        return result;
    }

    @OperLog(operModul = "平台-订单管理-订单详情", operDesc = "修改价格")
    @SupplierLoginCheck(alias = "SupplierOrderOperationAlias")
    @RequestMapping(value = "/modifyPrice")
    public Result<String> modifyPrice(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Integer skuOrderId = jsonObject.getInteger("skuOrderId");
        Integer skuOrderDetailId = jsonObject.getInteger("skuOrderDetailId");
        Double price = jsonObject.getDouble("price");
        Result<String> result = Result.success(null);
        try {
            skuOrderService.modifyPrice(skuOrderId,skuOrderDetailId, price);
        } catch (BusinessException e) {
            e.printStackTrace();
        }
        result.setData("success");
        return result;
    }

    @OperLog(operModul = "平台-订单管理-订单详情", operDesc = "修改价格")
    @SupplierLoginCheck(alias = "SupplierOrderOperationAlias")
    @RequestMapping(value = "/examineWaitingCancel")
    public Result<String> examineWaitingCancel(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Integer skuOrderId = jsonObject.getInteger("skuOrderId");
        String status = jsonObject.getString("status");
        String refuseRefundReason = jsonObject.getString("refuseRefundReason");
        Result<String> result = Result.success(null);
        try {
            skuOrderService.examineWaitingCancel(skuOrderId, status, refuseRefundReason);
        } catch (BusinessException e) {
            e.printStackTrace();
        }
        result.setData("success");
        return result;
    }

    @OperLog(operModul = "平台-订单管理-上传物流单号", operDesc = "订单详情")
    @SupplierLoginCheck(alias = "SupplierOrderOperationAlias")
    @RequestMapping(value = "/setDeliveryDemand")
    public Result<String> setDeliveryDemand(HttpServletRequest request, @RequestBody SkuOrderDeliveryDemandResult req) {
        Result<String> result = Result.success(null);
        try {
            WeiXinPushResult weiXinPushResult = skuOrderService.setDeliveryDemand(req);
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Object accessToken = redisService.get("accessToken");
                        logger.info("firstAccessToken===" + JSONObject.toJSONString(accessToken));
                        String accessTokenStr = null;
                        if (accessToken == null) {
                            JSONObject accessToken1 = WeixinGzhAppletsUtil.getGzhAccessToken();
                            logger.info("accessToken1===" + JSONObject.toJSONString(accessToken1));
                            accessToken1.put("expires_time", DateUtil.addSeconds(new Date(), accessToken1.getInteger("expires_in")));
                            redisService.set("accessToken", accessToken1);
                            accessTokenStr = accessToken1.getString("access_token");
                        } else {
                            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(accessToken));
                            logger.info("accessToken===" + JSONObject.toJSONString(accessToken));
                            if (jsonObject.getDate("expires_time").before(new Date())) {
                                JSONObject accessToken2 = WeixinGzhAppletsUtil.getGzhAccessToken();
                                logger.info("accessToken2===" + JSONObject.toJSONString(accessToken2));
                                accessToken2.put("expires_time", DateUtil.addSeconds(new Date(), accessToken2.getInteger("expires_in")));
                                redisService.set("accessToken", accessToken2);
                                accessTokenStr = accessToken2.getString("access_token");
                            } else {
                                accessTokenStr = jsonObject.getString("access_token");
                            }
                        }
                        if (StringUtils.isNotEmpty(weiXinPushResult.getOpenId())) {
                            Map<String, String> content = new HashMap<>();
                            content.put("inquireOrderId", weiXinPushResult.getInquireOrderId() + "");
                            content.put("skuName", weiXinPushResult.getSkuName());
                            content.put("status", weiXinPushResult.getStatus());
                            content.put("createTime", DateUtil.getDateTime(weiXinPushResult.getCreateTime()));
                            WeixinGzhAppletsUtil.push("uhgZX_euKA6gvV66W7QroYGubIoJZnE4p3GWiK_2ZgQ"
                                    , accessTokenStr, weiXinPushResult.getOpenId(), content, "`pages/home/index?urlData=` + encodeURIComponent(JSON.stringify(`/pages/home/subPages/product-detail/index?id=" + weiXinPushResult.getInquireOrderId() + "`))");
                        }
                    } catch (BusinessException e) {
                        e.printStackTrace();
                        logger.error("#tongliSignContract# error" + e);
                    } catch (Exception e) {
                        e.printStackTrace();
                        logger.error("#tongliSignContract# error" + e);
                    }
                }
            });

        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.failed(e.getCode(), e.getMessage());
        }
        result.setData("SUCCESS");
        return result;
    }

    @OperLog(operModul = "平台-订单管理-设置物流费用", operDesc = "设置物流费用")
    @SupplierLoginCheck(alias = "SupplierOrderOperationAlias")
    @RequestMapping(value = "/setPostage")
    public Result<String> setPostage(HttpServletRequest request, @RequestBody SkuOrderDeliveryDemandResult req) {
        Result<String> result = Result.success(null);
        try {
            WeiXinPushResult weiXinPushResult = skuOrderService.setPostage(req);
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Object accessToken = redisService.get("accessToken");
                        String accessTokenStr = null;
                        if (accessToken == null) {
                            JSONObject accessToken1 = WeixinGzhAppletsUtil.getGzhAccessToken();
                            logger.info("accessToken1===" + JSONObject.toJSONString(accessToken1));
                            accessToken1.put("expires_time", DateUtil.addSeconds(new Date(), accessToken1.getInteger("expires_in")));
                            redisService.set("accessToken", accessToken1);
                            accessTokenStr = accessToken1.getString("access_token");
                        } else {
                            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(accessToken));
                            logger.info("accessToken===" + JSONObject.toJSONString(accessToken));
                            if (jsonObject.getDate("expires_time").before(new Date())) {
                                JSONObject accessToken2 = WeixinGzhAppletsUtil.getGzhAccessToken();
                                logger.info("accessToken2===" + JSONObject.toJSONString(accessToken2));
                                accessToken2.put("expires_time", DateUtil.addSeconds(new Date(), accessToken2.getInteger("expires_in")));
                                redisService.set("accessToken", accessToken2);
                                accessTokenStr = accessToken2.getString("access_token");
                            } else {
                                accessTokenStr = jsonObject.getString("access_token");
                            }
                        }
                        if (StringUtils.isNotEmpty(weiXinPushResult.getOpenId())) {
                            Map<String, String> content = new HashMap<>();
                            content.put("inquireOrderId", weiXinPushResult.getInquireOrderId() + "");
                            content.put("skuName", weiXinPushResult.getSkuName());
                            content.put("status", weiXinPushResult.getStatus());
                            content.put("createTime", DateUtil.getDateTime(weiXinPushResult.getCreateTime()));
                            WeixinGzhAppletsUtil.push("uhgZX_euKA6gvV66W7QroYGubIoJZnE4p3GWiK_2ZgQ"
                                    , accessTokenStr, weiXinPushResult.getOpenId(), content, "`pages/home/index?urlData=` + encodeURIComponent(JSON.stringify(`/pages/home/subPages/product-detail/index?id=" + weiXinPushResult.getInquireOrderId() + "`))");
                        }
                    } catch (BusinessException e) {
                        e.printStackTrace();
                        logger.error("#tongliSignContract# error" + e);
                    }
                }
            });
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.failed(e.getCode(), e.getMessage());
        }
        result.setData("SUCCESS");
        return result;
    }


    @OperLog(operModul = "平台-订单管理-审核退款", operDesc = "审核退款")
    @SupplierLoginCheck(alias = "SupplierOrderOperationAlias")
    @RequestMapping(value = "/setApply")
    public Result<String> setApply(HttpServletRequest request, @RequestBody SkuOrderDeliveryDemandResult req) {
        Result<String> result = Result.success(null);
        try {
            SkuApplyOrderResult skuApplyOrderResult = skuOrderService.examineRefund(getSupplier().getSupplierUserId(), req.getSkuOrderDetailId(), req.getStatus(), req.getRefuseRefundReason(), RoleTypeEnum.SUPPLIER.getCode());
            if (skuApplyOrderResult != null && SkuOrderStatusEnum.PASS.getCode().equals(skuApplyOrderResult.getStatus()) && skuApplyOrderResult.getTotalFee() > 0.0) {
                WXPayUtil.wxPayRefund(skuApplyOrderResult.getAppletType(), webDomain + "/weixin/refund/notify", skuApplyOrderResult.getBillNo(), skuApplyOrderResult.getTotalFee(), skuApplyOrderResult.getRefundFee(), skuApplyOrderResult.getRefundNo());
            }
            WeiXinPushResult weiXinPushResult = skuApplyOrderResult.getWeiXinPushResult();
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Object accessToken = redisService.get("accessToken");
                        String accessTokenStr = null;
                        if (accessToken == null) {
                            JSONObject accessToken1 = WeixinGzhAppletsUtil.getGzhAccessToken();
                            logger.info("accessToken1===" + JSONObject.toJSONString(accessToken1));
                            accessToken1.put("expires_time", DateUtil.addSeconds(new Date(), accessToken1.getInteger("expires_in")));
                            redisService.set("accessToken", accessToken1);
                            accessTokenStr = accessToken1.getString("access_token");
                        } else {
                            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(accessToken));
                            logger.info("accessToken===" + JSONObject.toJSONString(accessToken));
                            if (jsonObject.getDate("expires_time").before(new Date())) {
                                JSONObject accessToken2 = WeixinGzhAppletsUtil.getGzhAccessToken();
                                logger.info("accessToken2===" + JSONObject.toJSONString(accessToken2));
                                accessToken2.put("expires_time", DateUtil.addSeconds(new Date(), accessToken2.getInteger("expires_in")));
                                redisService.set("accessToken", accessToken2);
                                accessTokenStr = accessToken2.getString("access_token");
                            } else {
                                accessTokenStr = jsonObject.getString("access_token");
                            }
                        }
                        if (StringUtils.isNotEmpty(weiXinPushResult.getOpenId())) {
                            Map<String, String> content = new HashMap<>();
                            content.put("inquireOrderId", weiXinPushResult.getInquireOrderId() + "");
                            content.put("skuName", weiXinPushResult.getSkuName());
                            content.put("status", weiXinPushResult.getStatus());
                            content.put("createTime", DateUtil.getDateTime(weiXinPushResult.getCreateTime()));
                            WeixinGzhAppletsUtil.push("uhgZX_euKA6gvV66W7QroYGubIoJZnE4p3GWiK_2ZgQ"
                                    , accessTokenStr, weiXinPushResult.getOpenId(), content, "`pages/home/index?urlData=` + encodeURIComponent(JSON.stringify(`/pages/home/subPages/product-detail/index?id=" + weiXinPushResult.getInquireOrderId() + "`))+");
                        }
                        if (StringUtils.isNotEmpty(weiXinPushResult.getOpenId()) && weiXinPushResult.getIntegral() > 0) {
                            Map<String, String> mapContent = new HashMap<>();
                            mapContent.put("integral", weiXinPushResult.getIntegral() + "");
                            mapContent.put("remark", weiXinPushResult.getRemark());
                            mapContent.put("createTime", DateUtil.getDateTime(weiXinPushResult.getCreateTime()));
                            WeixinGzhAppletsUtil.pushIntegral("v49YEpK9mS2XAqOM41YTiOLUUIKOz61ooYDc52koXPk"
                                    , accessTokenStr, weiXinPushResult.getOpenId(), mapContent);
                        }
                    } catch (BusinessException e) {
                        e.printStackTrace();
                        logger.error("#tongliSignContract# error" + e);
                    }
                }
            });
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(9999, e.getMessage());
        }
        result.setData("SUCCESS");
        return result;
    }

    @OperLog(operModul = "平台-订单管理-直接退款", operDesc = "直接退款")
    @SupplierLoginCheck(alias = "SupplierOrderOperationAlias")
    @RequestMapping(value = "/setDirectApply")
    public Result<String> setDirectApply(HttpServletRequest request, @RequestBody SkuOrderDeliveryDemandResult req) {
        Result<String> result = Result.success(null);
        try {
            SkuApplyOrderResult skuApplyOrderResult = skuOrderService.examineRefundByDirect(getSupplier().getSupplierUserId(), req.getSkuOrderDetailId(), req.getRefuseRefundReason(), RoleTypeEnum.SUPPLIER.getCode(), getSupplier().getSupplierId());
            if (skuApplyOrderResult != null && SkuOrderStatusEnum.PASS.getCode().equals(skuApplyOrderResult.getStatus()) && skuApplyOrderResult.getTotalFee() > 0.0) {
                WXPayUtil.wxPayRefund(skuApplyOrderResult.getAppletType(), webDomain + "/weixin/refund/notify", skuApplyOrderResult.getBillNo(), skuApplyOrderResult.getTotalFee(), skuApplyOrderResult.getRefundFee(), skuApplyOrderResult.getRefundNo());
            }
            WeiXinPushResult weiXinPushResult = skuApplyOrderResult.getWeiXinPushResult();
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Object accessToken = redisService.get("accessToken");
                        String accessTokenStr = null;
                        if (accessToken == null) {
                            JSONObject accessToken1 = WeixinGzhAppletsUtil.getGzhAccessToken();
                            logger.info("accessToken1===" + JSONObject.toJSONString(accessToken1));
                            accessToken1.put("expires_time", DateUtil.addSeconds(new Date(), accessToken1.getInteger("expires_in")));
                            redisService.set("accessToken", accessToken1);
                            accessTokenStr = accessToken1.getString("access_token");
                        } else {
                            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(accessToken));
                            logger.info("accessToken===" + JSONObject.toJSONString(accessToken));
                            if (jsonObject.getDate("expires_time").before(new Date())) {
                                JSONObject accessToken2 = WeixinGzhAppletsUtil.getGzhAccessToken();
                                logger.info("accessToken2===" + JSONObject.toJSONString(accessToken2));
                                accessToken2.put("expires_time", DateUtil.addSeconds(new Date(), accessToken2.getInteger("expires_in")));
                                redisService.set("accessToken", accessToken2);
                                accessTokenStr = accessToken2.getString("access_token");
                            } else {
                                accessTokenStr = jsonObject.getString("access_token");
                            }
                        }
                        if (StringUtils.isNotEmpty(weiXinPushResult.getOpenId())) {
                            Map<String, String> content = new HashMap<>();
                            content.put("inquireOrderId", weiXinPushResult.getInquireOrderId() + "");
                            content.put("skuName", weiXinPushResult.getSkuName());
                            content.put("status", weiXinPushResult.getStatus());
                            content.put("createTime", DateUtil.getDateTime(weiXinPushResult.getCreateTime()));
                            WeixinGzhAppletsUtil.push("uhgZX_euKA6gvV66W7QroYGubIoJZnE4p3GWiK_2ZgQ"
                                    , accessTokenStr, weiXinPushResult.getOpenId(), content, "`pages/home/index?urlData=` + encodeURIComponent(JSON.stringify(`/pages/home/subPages/product-detail/index?id=" + weiXinPushResult.getInquireOrderId() + "`))+");
                        }
                        if (StringUtils.isNotEmpty(weiXinPushResult.getOpenId()) && weiXinPushResult.getIntegral() > 0) {
                            Map<String, String> mapContent = new HashMap<>();
                            mapContent.put("integral", weiXinPushResult.getIntegral() + "");
                            mapContent.put("remark", weiXinPushResult.getRemark());
                            mapContent.put("createTime", DateUtil.getDateTime(weiXinPushResult.getCreateTime()));
                            WeixinGzhAppletsUtil.pushIntegral("v49YEpK9mS2XAqOM41YTiOLUUIKOz61ooYDc52koXPk"
                                    , accessTokenStr, weiXinPushResult.getOpenId(), mapContent);
                        }
                    } catch (BusinessException e) {
                        e.printStackTrace();
                        logger.error("#tongliSignContract# error" + e);
                    }
                }
            });
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(9999, e.getMessage());
        }
        result.setData("SUCCESS");
        return result;
    }

    @RequestMapping(value = "/delete/key")
    public Result<String> delete(HttpServletRequest request) {
        Result<String> result = Result.success(null);
        String key = request.getParameter("key");
        redisService.del(key);
        return result;
    }

    @RequestMapping(value = "/remind/enjoy/job", method = {RequestMethod.POST})
    public Result<String> remindEnjoyOrder(HttpServletRequest request) {
        Result<String> result = Result.success(null);
        logger.info("进来了====");
        List<Map<String, String>> list = skuOrderService.remindEnjoyOrder(new Date());
        if (list.size() > 0) {
            for (Map<String, String> stringObjectMap : list) {
                logger.info("merchantId===" + stringObjectMap.get("merchantId"));
                if (org.apache.commons.lang3.StringUtils.isNoneBlank(stringObjectMap.get("openId"))) {
                    try {
                        Object accessToken = redisService.get("accessToken");
                        String accessTokenStr = null;
                        if (accessToken == null) {
                            JSONObject accessToken1 = com.coffee.admin.utils.WeixinGzhAppletsUtil.getGzhAccessToken();
                            accessToken1.put("expires_time", DateUtil.addSeconds(new Date(), accessToken1.getInteger("expires_in")));
                            redisService.set("accessToken", accessToken1);
                            accessTokenStr = accessToken1.getString("access_token");
                        } else {
                            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(accessToken));
                            if (jsonObject.getDate("expires_time").before(new Date())) {
                                JSONObject accessToken2 = com.coffee.admin.utils.WeixinGzhAppletsUtil.getGzhAccessToken();
                                accessToken2.put("expires_time", DateUtil.addSeconds(new Date(), accessToken2.getInteger("expires_in")));
                                redisService.set("accessToken", accessToken2);
                                accessTokenStr = accessToken2.getString("access_token");
                            } else {
                                accessTokenStr = jsonObject.getString("access_token");
                            }
                        }
                        Map<String, String> content = new HashMap<>();
                        content.put("inquireOrderId", stringObjectMap.get("date"));
                        content.put("skuName", stringObjectMap.get("enjoyDay"));
                        content.put("status", SkuOrderStatusEnum.WAITING.getName());
                        content.put("createTime", stringObjectMap.get("date"));
                        com.coffee.admin.utils.WeixinGzhAppletsUtil.push("uhgZX_euKA6gvV66W7QroYGubIoJZnE4p3GWiK_2ZgQ"
                                , accessTokenStr, stringObjectMap.get("openId"), content, "`pages/home/index?urlData=` + encodeURIComponent(JSON.stringify(`pages/mine/subPages/lazy-pay/index`))");

                    } catch (BusinessException e) {
                        e.printStackTrace();
                        logger.error("#tongliSignContract# error" + e);
                    } catch (Exception e) {
                        e.printStackTrace();
                        logger.error("#tongliSignContract# error" + e);
                    }
                }
            }
        }
        result.setData("success");
        return result;
    }


    @RequestMapping(value = "/remind/periodization/job", method = {RequestMethod.POST})
    public Result<String> remindPeriodizationOrder(HttpServletRequest request) {
        Result<String> result = Result.success(null);
        logger.info("进来了====");
        List<Map<String, String>> list = skuOrderService.remindPeriodizationOrder(new Date());
        if (list.size() > 0) {
            for (Map<String, String> stringObjectMap : list) {
                logger.info("merchantId===" + stringObjectMap.get("merchantId"));
                if (org.apache.commons.lang3.StringUtils.isNoneBlank(stringObjectMap.get("openId"))) {
                    try {
                        Object accessToken = redisService.get("accessToken");
                        String accessTokenStr = null;
                        if (accessToken == null) {
                            JSONObject accessToken1 = com.coffee.admin.utils.WeixinGzhAppletsUtil.getGzhAccessToken();
                            accessToken1.put("expires_time", DateUtil.addSeconds(new Date(), accessToken1.getInteger("expires_in")));
                            redisService.set("accessToken", accessToken1);
                            accessTokenStr = accessToken1.getString("access_token");
                        } else {
                            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(accessToken));
                            if (jsonObject.getDate("expires_time").before(new Date())) {
                                JSONObject accessToken2 = com.coffee.admin.utils.WeixinGzhAppletsUtil.getGzhAccessToken();
                                accessToken2.put("expires_time", DateUtil.addSeconds(new Date(), accessToken2.getInteger("expires_in")));
                                redisService.set("accessToken", accessToken2);
                                accessTokenStr = accessToken2.getString("access_token");
                            } else {
                                accessTokenStr = jsonObject.getString("access_token");
                            }
                        }
                        Map<String, String> content = new HashMap<>();
                        content.put("inquireOrderId", stringObjectMap.get("date"));
                        content.put("skuName", "分期购商品可以购买了");
                        content.put("status", SkuOrderStatusEnum.WAITING.getName());
                        content.put("createTime", stringObjectMap.get("date"));
                        com.coffee.admin.utils.WeixinGzhAppletsUtil.push("uhgZX_euKA6gvV66W7QroYGubIoJZnE4p3GWiK_2ZgQ"
                                , accessTokenStr, stringObjectMap.get("openId"), content, "`pages/home/index?urlData=` + encodeURIComponent(JSON.stringify(`pages/mine/subPages/lazy-pay/index`))");

                    } catch (BusinessException e) {
                        e.printStackTrace();
                        logger.error("#tongliSignContract# error" + e);
                    } catch (Exception e) {
                        e.printStackTrace();
                        logger.error("#tongliSignContract# error" + e);
                    }
                }
            }
        }
        result.setData("success");
        return result;
    }

}
